package com.jiutong.meipenggang.ui.activitys;


import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.base.AppManager;
import com.jiutong.meipenggang.base.BaseActivity;
import com.jiutong.meipenggang.base.CommonAdapter;
import com.jiutong.meipenggang.base.ViewHolder;
import com.jiutong.meipenggang.entity.RepaymentPlanBean;
import com.jiutong.meipenggang.http.model.interfaces.RepaymentPlanBiz;
import com.jiutong.meipenggang.http.present.RepaymentPlanPresent;
import com.jiutong.meipenggang.views.MyListView;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class InvestRepaymentAcitivty extends BaseActivity implements RepaymentPlanBiz {


    @Bind(R.id.rl_common_title_back)
    RelativeLayout rlCommonTitleBack;
    @Bind(R.id.tv_common_title)
    TextView tvCommonTitle;
    @Bind(R.id.tv_repayment_plan_way)
    TextView tvRepaymentPlanWay;
    @Bind(R.id.tv_repayment_plan_times)
    TextView tvRepaymentPlanTimes;
    @Bind(R.id.lv_repayment_money)
    MyListView lvRepaymentMoney;

    private RepaymentMoneyAdapter adapter;

    private RepaymentPlanPresent repaymentPlanPresent;
    private List<RepaymentPlanBean.ListBean> listBeen = new ArrayList<>();
    private int bidId;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_invest_repayment_acitivty;
    }

    @Override
    protected void initView() {
        super.initView();
        tvCommonTitle.setText("还款计划");

        Bundle bundle = getIntent().getExtras();
        bidId = bundle.getInt("bidId");
        if(bidId<=0){
            return;
        }

        repaymentPlanPresent = new RepaymentPlanPresent(this,this);
        repaymentPlanPresent.getRepaymentPlanInfo();


    }

    @OnClick(R.id.rl_common_title_back)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_common_title_back:
                AppManager.getAppManager().finishActivity(this);
                break;
            default:
        }
    }

    @Override
    public int bidId() {
        return bidId;
    }

    @Override
    public void onSuccess(Object object) {
        RepaymentPlanBean bean = (RepaymentPlanBean) object;
        tvRepaymentPlanTimes.setText(bean.getRepaySize()+"次");
        tvRepaymentPlanWay.setText(bean.getRepayName());
        listBeen = bean.getList();
        Logger.d("TAG",listBeen.size()+"");
        if(listBeen.size()>0){
            adapter = new RepaymentMoneyAdapter(this,listBeen,R.layout.item_repayment_money);
            lvRepaymentMoney.setAdapter(adapter);
        }
    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public int getOpt() {
        return 55;
    }

    public class RepaymentMoneyAdapter extends CommonAdapter<RepaymentPlanBean.ListBean> {

        public RepaymentMoneyAdapter(Context context, List<RepaymentPlanBean.ListBean> listDatas, int layoutId) {
            super(context, listDatas, layoutId);
        }

        @Override
        protected void fillData(ViewHolder holder, int position) {
            TextView tv_time = holder.getView(R.id.tv_item_repayment_time);
            TextView tv_money = holder.getView(R.id.tv_item_repayment_money);
            tv_time.setText(listDatas.get(position).getRepayTime());
            tv_money.setText("利息："+listDatas.get(position).getRepayInterest()+"元");
        }
    }
}
